package com.example.easybuy.controller;

import com.alibaba.fastjson.JSONObject;
import com.example.easybuy.pojo.News;
import com.example.easybuy.pojo.Page;
import com.example.easybuy.service.NewsService;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.List;

@Controller
public class NewsController {

    private Logger logger=Logger.getLogger(getClass());

    @Autowired
    private NewsService newsService;

    /*
    查询新闻标题前五条
     */
    @RequestMapping("getNewsFive")
    @ResponseBody
    @CrossOrigin
    public List<News> getNewsFive(){
        logger.debug("NewsController-getNewsFive()...start...");
        Page page=new Page();
        page.setCurrentPageNo(1);
        List<News> newsList=newsService.getNewsFive(page);
        logger.debug("NewsController-getNewsFive()-newsList："+newsList.toString());
//        List newsTitle=new ArrayList();
//        for (News news:newsList) {
//            newsTitle.add(news.getTitle());
//        }
//        System.out.println("newsTile:"+newsTitle.toString());
        return newsList;
    }

    /*
    新闻管理页，分页查询
     */
    @RequestMapping("getPageNews")
    @ResponseBody
    @CrossOrigin
    public Object getPageNews(Page page){
        logger.debug("NewsController-getPageNews()...start...page:"+page.getCurrentPageNo());
        List<News> newsList=newsService.getNewsFive(page);
        page.setDataList(newsList);
        logger.debug("NewsController-getPageNews()-newsList:"+newsList.toString());
        return page;
    }

    /*
    根据id查询一条新闻详情
     */
    @RequestMapping("getNewsOne")
    @ResponseBody
    @CrossOrigin
    public Object getNewsOne(String id){
        logger.debug("NewsController-getNewsOne()...start...查询的新闻id:"+id);
        News news=newsService.getNewsOne(id);
        logger.debug("NewsController-getNewsOne()-news:"+news.toString());
        return news;
    }

    /*
    根据id删除新闻
     */
    @RequestMapping("deletNews")
    @ResponseBody
    @CrossOrigin
    public String deletNews(String id){
        logger.debug("NewsController-deletNews()...start...要删除的新闻id:"+id);
        String success=newsService.deletNews(id);
        logger.debug("NewsController-deletNews()-success:"+success);
        return success;
    }

    /*
    添加新闻
     */
    @RequestMapping("addNews")
    @CrossOrigin
    @ResponseBody
    public void addNews(News news){
        logger.debug("NewsController-addNews()...start...news:"+news);
        newsService.addNews(news);
    }

    /*
    根据id修改新闻
     */
    @RequestMapping("modifyNews")
    @ResponseBody
    @CrossOrigin
    public void modifyNews(News news){
        logger.debug("NewsController-modifyNews()...start...news:"+news);
        newsService.modifyNews(news);
    }


}
